Imports System
Imports System.Xml

Public Class TradersMsg
	Inherits BaseMsg

	'<CPCAPI v="1.1">
	'      <PtyReq ReqID="testID">
	'            <Pty R="1" ID="abn"/>
	'      </PtyReq>
	'</CPCAPI>
	'<CPCAPI v="1.1">
	'      <PtyRsp ReqID="testID">
	'            <Pty R="1" ID="abn"/>
	'            <Pty ID="tmcgrath" R="12">
	'                  <Sub ID="Thomas McGrath" Typ="9"/>
	'            </Pty>
	'            <Pty ID="jjbrady" R="12">
	'                  <Sub ID="John J Brady" Typ="9"/>
	'            </Pty>
	'            <Pty ID="opex_test_112007" R="12">
	'                  <Sub ID="Shalini Gupta" Typ="9"/>
	'            </Pty>
	'      </PtyRsp>
	'</CPCAPI>
	Protected m_firm As String

	Public Property FirmID() As String
		Get
			Return m_firm
		End Get
		Set(ByVal Value As String)
			m_firm = Value
		End Set
	End Property

	Public Sub New()
		RequestType = "PtyReq"
		ResponseType = "PtyRsp"
	End Sub

	Protected Overrides Function RequestMsg(ByRef XmlW As XmlTextWriter) As Boolean

		If m_firm = "" Then
			Return False
		End If

		XmlW.WriteStartElement("Pty")

		XmlW.WriteAttributeString("R", "1")
		XmlW.WriteAttributeString("ID", m_firm)

		XmlW.WriteEndElement()

		Return True
	End Function

	Protected Overrides Sub ResponseParse(ByRef node As XmlNode)

		Dim ptygrp As XmlNode
		Dim attr As XmlAttribute

		Dim dbi As dbInterface = Globals.dbi

		'		If node.Attributes("ProductID").InnerText = m_prodid Then

		dbi.nonquery("delete from nymx_firm_traders where firm_id=" & dbi.quote(m_firm))

		For Each ptygrp In node.ChildNodes
			InsertTrader(ptygrp)
		Next
		'		End If

	End Sub

	Private Sub InsertTrader(ByRef party As XmlNode)
		'            <Pty R="1" ID="abn"/>
		'            <Pty ID="tmcgrath" R="12">
		'                  <Sub ID="Thomas McGrath" Typ="9"/>
		'            </Pty>

		Dim dbi As dbInterface = Globals.dbi
		Dim ptysub As XmlNode

		Dim id As String = ""
		Dim r As String
		Dim nm As String = ""

		If party.Name = "Pty" Then
			r = party.Attributes("R").InnerText

			If r = "1" Then
				Exit Sub
			End If

			If party.ChildNodes.Count > 0 Then
				For Each ptysub In party.ChildNodes
					If ptysub.Name = "Sub" Then
						nm = ptysub.Attributes("ID").InnerText
					End If
				Next
			End If

			id = party.Attributes("ID").InnerText

			dbi.SetupStoredProc("sp_nymx_ins_firm_trader")
			dbi.AddStoredProcParam("@firm_id", SqlDbType.VarChar, m_firm)
			dbi.AddStoredProcParam("@trader_id", SqlDbType.VarChar, id)
			dbi.AddStoredProcParam("@trader_nm", SqlDbType.VarChar, nm)
			dbi.RunStoredProc()

		End If

	End Sub

End Class
